﻿new Vue({
  el: '#main',
  data: {
    name: '',
    nameText:{state:0,des:''},
    username: '',
    usernameText:{state:0,des:''},
    password: '',
    passwordSwitch:true,
    passwordText:{state:0,des:''},
    rePassword: '',
    rePasswordText:{state:0,des:''},
    checkAgree:false,
    registerSwitch:true,
  },
  methods: {
    checkName(prop,value) {
      if(prop=='name'&&(!value||value=='')) return this[prop+'Text'] = {state:1,des:'请告诉我你的昵称吧'};
      if(prop=='name'&&!this.checkname(value)) return this[prop+'Text'] = {state:1,des:'只能输入汉字、英文、数字'};
      if(prop=='name'&&!this.checkNameLength(value)) return this[prop+'Text'] = {state:1,des:'昵称不能超过8个字符哦'};
      if(prop=='username'&&(!value||value=='')) return this[prop+'Text'] = {state:1,des:'请告诉我你的手机号吧'};
      this.$http.post('/moongoDB/checkName',{
        [prop]:value,
      }).then(resp=>{
        this[prop+'Text'] = resp.body;
      })
    },

    checkPassword(e){
      if(this.password.length>16||this.password.length<6) this.passwordText = {state:1,des:'密码由6-16个字符组成哦'};
      else this.passwordText = {state:0,des:''};
    },
    checkrePassword(e){
      if(this.rePassword.length < this.password.length && e.keyCode!=8)
      return false;
      if(this.rePassword == this.password)   this.rePasswordText = {state:0,des:''} 
      else this.rePasswordText = {state:1,des:'密码不一致哦亲'}
    },
    chcek_Agree(){
      this.checkAgree = !this.checkAgree;
    },

    checkPhoneNo(o){
      var re=/^1[3456789]\d{9}$/;
      if(!re.test(o)){
        return false;
      }else{
        return true;
      }
    },

    checkNameLength(o){
      if(this.strlen(o)>8) return false;
      else return true;
    },
    // 字符长度
    strlen(str){  
      var len = 0;  
      for (var i=0; i<str.length; i++) {   
       var c = str.charCodeAt(i);   
      //单字节加1   
       if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {   
         len++;   
       }   
       else {   
        len+=2;   
       }   
      }   
      return len;  
    },

    checkname(o){
      var re=/^[\u4E00-\u9FA5A-Za-z0-9]+$/;
      if(!re.test(o)){
        return false;
      }else{
        return true;
      }
    },

    toLogin() {
      window.location.href = '/login'
    },

    toRegister() {
      if(!this.registerSwitch) return false;
      this.checkPassword();this.checkrePassword();this.checkName('name',this.name);
      if(this.nameText.state==1||this.usernameText.state==1||this.passwordText.state==1||this.rePasswordText.state==1||!this.checkAgree)
      return false;
      if(!this.checkPhoneNo(this.username)) return this.usernameText = {state:1,des:'手机号码不规范'};
      this.registerSwitch = false;
      this.$http.post('/moongoDB/register',{
        name:this.name,
        username:this.username,
        password:this.password,
      }).then(resp=>{
        var type = 'success';
        var that = this;
        noticeDiv(resp.body,type,2000,function(){
          window.location.href = `/login`;
          that.registerSwitch = true;
        });  
      })
    }

  },

  created() {

  },
  updated() {

  },

});